import { Injectable } from '@angular/core';

@Injectable()
export class ProductService {

  private products: Product[] = [
    new Product(1, '第一个商品', 1.99, 3.5, '这是一个商品描述,哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈', ['电子产品', '硬件']),
    new Product(2, '第二个商品', 3.99, 2.5, '这是一个商品描述,哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈', ['图书']),
    new Product(3, '第三个商品', 9.99, 4.5, '这是一个商品描述,哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈', ['硬件']),
    new Product(4, '第四个商品', 8.99, 3.5, '这是一个商品描述,哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈', ['电子产品', '硬件']),
    new Product(5, '第五个商品', 7.99, 1.5, '这是一个商品描述,哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈', ['电子产品']),
    new Product(6, '第六个商品', 6.99, 4.5, '这是一个商品描述,哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈', ['电子产品', '硬件'])
    ];

  private comments: Comment[] = [
    new Comment(1, 1, '2017-04-09 11:11:11', '张三', 3, '当量不错'),
    new Comment(2, 1, '2017-03-09 11:11:11', '李四', 3.5, '当量不错'),
    new Comment(3, 6, '2017-04-09 11:11:11', '王五', 4, '当量不错'),
    new Comment(4, 5, '2017-01-09 11:11:11', '赵六', 4.5, '当量不错'),
    new Comment(5, 4, '2017-01-09 11:11:11', '陈琦', 3, '当量不错'),
    new Comment(6, 3, '2015-04-09 11:11:11', 'jack', 2, '当量不错'),
    new Comment(7, 2, '2016-04-09 11:11:11', 'aron', 2.5, '当量不错'),
    new Comment(8, 1, '2017-04-09 11:11:11', 'angle', 5, '当量不错'),
  ];

    constructor() {

    }

  getProducts(): Product[] {
    return this.products;
  }

  getProduct(id: number): Product {
      return this.products.find((product) => {
        return product.id == id;
      });
  }

  getCommentForProductId(id: number): Comment[] {
    return this.comments.filter((comment: Comment) => {
      return comment.productId == id;
    });
  }
}

/**
 * 商品对象
 */
export class Product {
  // 构造函数
  constructor(
    public id: number,
    public title: string,
    public price: number,
    public rating: number,
    public desc: string,
    public categories: Array<string>
  ){

  }
}
export class Comment {
  constructor(
    public id: number,
    public productId: number,
    public timestamp: string,
    public user: string,
    public rating: number,
    public content: string
  ) {

  }
}
